<?php
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
class User extends Base
{
    public $weekday = [];
    public function initialize() {
        parent::initialize();
        $returnUrl=Url::build('dboard/mlogin/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0 || !isset($this->AdminAccount['sid'])){
            if($this->request->isAjax()){
                return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
            }
            return $this->redirect($returnUrl);
        }
        $this->weekday = [1=>'一',2=>'二',3=>'三',4=>'四',5=>'五',6=>'六',7=>'日'];
    }
    public function add(){
        return view('user/add');
    }
    public function edit(){
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }
        $course = Db::name('course')
              ->field('*')
              ->where(['status'=>1,'sid'=>$this->AdminAccount['sid']])
              ->order('id desc')->select();
        if (count($course)) {
            foreach ($course as $key => &$value) {
                $value['class_time_str'] = '';
                if ($value['class_time']) {
                    $arr = explode('|', $value['class_time']);
                    if ($arr) {
                        foreach ($arr as $k => $v) {
                            if ($k>0) $value['class_time_str'] .= '，';
                            $arr_arr = explode(',', $v);
                            if (count($arr_arr)==3) $value['class_time_str'] .= '每周'.$this->weekday[$arr_arr[0]].' '.$arr_arr[1].'-'.$arr_arr[2];
                        }
                    }
                }
            }
        }
        $info = Db::name('user')
                ->where('id',$id)
                ->find();
        $info['course1'] = Db::name('user_course')->where(['user_id'=>$id,'status'=>2])->value('group_concat(course_id)');
        $info['course2'] = Db::name('user_course')->where(['user_id'=>$id,'status'=>1])->value('group_concat(course_id)');
        $this->assign('info',$info);
        $this->assign('course_list',$course);
        $this->assign('cid',$id);
        return view('user/edit');
    }
    public function lists(){
        return view('user/lists');
    }
   public function ajaxList(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $page_limit      = isset($input['length'])?$input['length']:10;
        $page_start      = isset($input['start'])?$input['start']:0;
        $p = 1;
        if ($page_start > 0) {
            $p = ceil($page_start / ($page_limit - 1));
        }
        $sid = isset($input['sid'])?$input['sid']:0;
        if (!$sid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
          $this->res_data['Msg'] = "无此门店";
          return $this->res_data;
        }
        $where = [];
        $where[] = ['sid','=',$sid];
        $where[] = ['status','=',1];
        $where[] = ['is_teacher','=',0];
        $field = '*';
        $count=Db::name('user')->where($where)->count();
        $list  = Db::name('user')
              ->field($field)
              ->where($where)
              ->order('id desc')
              ->limit($page_start, $page_limit)->select();
        foreach($list as $k=>&$v){
        }
        
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['user'=>$list,'Data'=>$list,'recordsTotal'=>$count];
        return $this->print_result($this->res_data);
    }
    public function infoData(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }
        $where = [];
        $where[] = ['id','=',$id];
        $info = Db::name('user')
                ->where($where)
                ->find();

        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['user'=>$info];
        return $this->print_result($this->res_data);
   }
   public function doCreateUser(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $user = Controller('User', 'logic');
        $result            = $user->create($param);
        return $this->print_result($result);
   }
   public function editUserinfo(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $user= Controller('User', 'logic');
        $result            = $user->edit($param);
        return $this->print_result($result);
   }
   public function delete(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $user = Controller('User', 'logic');
        $result            = $user->delete($param);
        return $this->print_result($result);
   }
}
